package com.angel.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.angel.activity.ActivityDetail;
import com.angel.activity.MainActivity;
import com.angel.activity.R;
import com.angel.entity.ActivityBean;
import com.angel.entity.ActivityBeanList;
import com.angel.util.AsyncDownload;
import com.angel.util.BaseUtil;
import com.angel.util.HttpUtil;

/*用于主推后动的ViewPager页面的Adapter*/
public class ImplePagerAdapter extends PagerAdapter{
	private List<ActivityBean> viewpagelist;
	private Activity activity;
	private LayoutInflater inflater;
	public ImplePagerAdapter(Activity activity,List<ActivityBean> viewpagelist){
		this.viewpagelist=viewpagelist;
		this.activity=activity;
		inflater=activity.getLayoutInflater();
	}
	@Override
	public int getCount() {
		return viewpagelist.size();
	}
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return (Object)arg0==arg1;
	}
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		      
		    ((ViewPager)container).removeView((View)object);
	}
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		Log.i("abc","ImplePagerAdapter"+position);
		 View v=container.getChildAt(position);
		 PagerHolder holder=null;
//		 if(v==null){
			 holder=new PagerHolder();
			 v=inflater.inflate(R.layout.item1, null);
			 holder.viewPageimage=(ImageView)v.findViewById(R.id.viewpage_img1);
			 holder. activityTitle=(TextView)v.findViewById(R.id.main_actititle);
			 holder.activityTime=(TextView)v.findViewById(R.id.main_acti_time);
			 holder. activityAddress=(TextView)v.findViewById(R.id.main_acti_address);
			 holder.paticipateNum=(TextView)v.findViewById(R.id.participate_num);
			 holder. interstNum=(TextView)v.findViewById(R.id.interest_num);
			 holder. styleText=(TextView)v.findViewById(R.id.main_acti_type);
			 
			 BaseUtil.setBoldText(holder.activityTitle);//设置中文字体为粗体
	   		 /*设置主页获取到图片及文本*/
//			        String picAddress=HttpUtil.picUrl+"angel/"+viewpagelist.get(position).getMainActivityImgUrl();
			 		String picAddress=BaseUtil.getPicUrl(activity,viewpagelist.get(position).getMainActivityImgUrl());
			        
			        holder.activityTitle.setText(viewpagelist.get(position).getMainActivityTitle());
			        holder.activityTime.setText(viewpagelist.get(position).getMainActivityTime());
			        String address=viewpagelist.get(position).getMainActivityAddress();
			        if(address.length()>15){
			        	address=address.substring(0, address.length()-4)+"...";
			        }
			        holder.activityAddress.setText(address);
			        holder.paticipateNum.setText(viewpagelist.get(position).getActivityPartipateNum());
			        holder.interstNum.setText(viewpagelist.get(position).getActivityInterestNum());
			        holder.styleText.setText(viewpagelist.get(position).getMainActivitytype());
	   				Log.i("LoginActivity","picAddress:"+picAddress);
	   				AsyncDownload asyncDown=AsyncDownload.getInstance();
	   				
	   				Drawable dr=asyncDown.imageDown(holder.viewPageimage,picAddress, new AsyncDownload.CallBack(){
						@Override
						public void imgDown(ImageView view,Drawable drawable) {
							view.setImageDrawable(drawable);					}
	   				});
	//   				if(dr==null){
	//   					viewPageimage.setBackgroundResource(R.drawable.angel);
	//   				}
	   			    if(dr!=null){
	   			    	holder.viewPageimage.setImageDrawable(dr);}
	   			    v.setTag(holder);
//		 }else{
////			 holder=(PagerHolder)v.getTag();
//		 }
		 ((ViewPager)container).addView(v);
		  v.setOnClickListener(new MyOnClickListener(this.activity,viewpagelist.get(position).getActivityId()));
		  return v;     
	}
}
/*响应点击ViewPager跳转界面*/
class MyOnClickListener implements OnClickListener{
	private Activity activity;
	private String activityId;
	public MyOnClickListener(Activity activity,String activityId){
		this.activityId=activityId;
		this.activity=activity;
	}
	public void onClick(View v){
		  Intent intent=new Intent(activity,ActivityDetail.class);
		  intent.putExtra("activityId",activityId );//传送活动Id
		  activity.startActivity(intent);
	}
}
class PagerHolder {
	 ImageView viewPageimage;
	 TextView activityTitle;
	 TextView activityTime;
	 TextView activityAddress;
	 TextView paticipateNum;
	 TextView interstNum;
	 TextView styleText;
}
